<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=BIG5">
    <title>Insert title here</title>
    
    <script>
    function submitAction(executeValue){
    	form.execute.value=executeValue;
    	form.submit();
    }
    </script>
</head>
<body>
<form id="form" name="form" method="post" action="<%=request.getContextPath()%>/employee.do">
    <input type="hidden" name="execute" value="">
    <input type="hidden" name="deleteId" value="">
    
    <!-- error message -->
    <spring:bind path="employeeModel.*">
       <c:if test="${not empty status.errorMessages}">
           <div class="error">    
               <c:forEach var="error" items="${status.errorMessages}" varStatus="status">
                   <li><span style="color: #FF0000;"><c:out value="${error}" escapeXml="false"/></span>
                   </li><br />
               </c:forEach>
           </div><br>
       </c:if>
    </spring:bind>    

    <div>
        <input type="button" value="新增" onclick="submitAction('add');">
        <input type="button" value="儲存" onclick="submitAction('save');">
        <input type="button" value="測試" onclick="submitAction('test');">
    </div>
    
    <div>
        <table border="1">
            <tr>
                <th>&nbsp;</th>
                <th>Employee No</th>
                <th>Name</th>
                <th>Department</th>
                <th>Sex</th>
                <th>Birthday</th>
                <th>Address</th>
                <th>Zip</th>
                <th>Create Date</th>
                <th>Delete</th>
            </tr>
            
            <c:forEach var="employee" items="${employeeModel.employees}" varStatus="s">
            <tr>
                <td>${s.index+1}&nbsp;</td>
                <td><spring:bind path="employeeModel.employees[${s.index}].empNo">
                        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].name">
                        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].deptNo">
                        <select id="${status.expression}" name="${status.expression}">
                            <c:forEach items="${deptSelectList}" var="entry">
                                <option value="${entry}" <c:if test="${status.value eq entry}">selected="selected"</c:if>>${entry}</option>
                            </c:forEach>
                        </select>
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].sex">
                        <select id="${status.expression}" name="${status.expression}">
                            <c:forEach items="${sexSelectMap}" var="entry">
                                <option value="${entry.key}" <c:if test="${status.value eq entry.key}">selected="selected"</c:if>>${entry.value}</option>
                            </c:forEach>
                        </select>
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].birthday">
                        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].address">
                        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
                    </spring:bind></td>
                <td><spring:bind path="employeeModel.employees[${s.index}].zip">
                        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
                    </spring:bind></td>
                <td>&nbsp;<fmt:formatDate value="${employee.createDate}" pattern="yyyy/MM/dd"/></td>
                <td><input type="button" value="刪除" onclick="form.deleteId.value='${s.index}';submitAction('delete');"></td>
            </tr>
            </c:forEach>
        </table>
    </div>
</form>
</body>
</html>